/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.web;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.common.sortList.SortList;
import com.jeeplus.common.utils.excel.ObjectExcelView;
import com.jeeplus.common.utils.excel.PageData;
import com.jeeplus.modules.tms.entity.Trucks;
import com.jeeplus.modules.tms.importEntity.TruckOwnerImport;
import com.jeeplus.modules.tms.mapper.TrucksMapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.tms.entity.TruckOwner;
import com.jeeplus.modules.tms.service.TruckOwnerService;

/**
 * 车辆Controller
 *
 * @author dhw
 * @version 2018-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/tms/truckOwner")
public class TruckOwnerController extends BaseController {

    @Autowired
    private TruckOwnerService truckOwnerService;
    @Autowired
    private TrucksMapper trucksMapper;

    @ModelAttribute
    public TruckOwner get(@RequestParam(required = false) String id) {
        TruckOwner entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = truckOwnerService.get(id);
        }
        if (entity == null) {
            entity = new TruckOwner();
        }
        return entity;
    }

    /**
     * 车辆列表页面
     */
    @RequiresPermissions("tms:truckOwner:list")
    @RequestMapping(value = {"list", ""})
    public String list() {
        TruckOwner truckOwner = new TruckOwner();
        List<TruckOwner> truckOwners = truckOwnerService.findList(truckOwner);
        for (TruckOwner truckOwner1 : truckOwners) {
            Trucks trucks = new Trucks();
            trucks.setTruckOwner(truckOwner1);
            List<Trucks> trucklists = trucksMapper.findList(trucks);
            HashSet set = new HashSet();
            for (Trucks i : trucklists)
                set.add(i.getNumber());
            if (!(set.size() == trucklists.size())) {
                System.out.println("有重复数据");
                truckOwner1.setIsrepeat("1");//1车辆有重复，列表颜色提示
            } else {
                truckOwner1.setIsrepeat("0");
            }
            truckOwnerService.save(truckOwner1);
        }
        return "modules/tms/truckOwnerList";
    }

    /**
     * 车辆列表数据
     */
    @ResponseBody
    @RequiresPermissions("tms:truckOwner:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(TruckOwner truckOwner, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TruckOwner> page = truckOwnerService.findPage(new Page<TruckOwner>(request, response), truckOwner);
        return getBootstrapData(page);
    }

    /**
     * 查看，增加，编辑车辆表单页面
     */
    @RequiresPermissions(value = {"tms:truckOwner:view", "tms:truckOwner:add", "tms:truckOwner:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(TruckOwner truckOwner, Model model) {
        model.addAttribute("truckOwner", truckOwner);
        return "modules/tms/truckOwnerForm";
    }

    /**
     * 保存车辆
     */
    @ResponseBody
    @RequiresPermissions(value = {"tms:truckOwner:add", "tms:truckOwner:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(TruckOwner truckOwner, Model model, RedirectAttributes redirectAttributes) throws Exception {
        AjaxJson j = new AjaxJson();
        if (!beanValidator(model, truckOwner)) {
            j.setSuccess(false);
            j.setMsg("非法参数！");
            return j;
        }
        List<TruckOwner> resourceList = truckOwnerService.findList(truckOwner);
        if (resourceList.size() == 0) {
            truckOwnerService.save(truckOwner);//保存
            j.setSuccess(true);
            j.setMsg("保存承运商成功");
            return j;
        } else {
            int i = 1;
            for (TruckOwner res : resourceList) {
                if ((res.getName()).equals((truckOwner.getName()))) {
                    i++;
                }
            }
            if (!truckOwner.getIsNewRecord()) {
                int k = 1;
                for (TruckOwner res : resourceList) {
                    if ((res.getName()).equals((truckOwner.getName())) && !((res.getId()).equals((truckOwner.getId())))) {
                        k++;
                    }
                }
                if (k == 1) {
                    truckOwnerService.save(truckOwner);//编辑保存
                    j.setSuccess(true);
                    j.setMsg("保存承运商成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("承运商已存在!");
                }
            } else {
                if (i == 1) {
                    truckOwnerService.save(truckOwner);//编辑保存
                    j.setSuccess(true);
                    j.setMsg("保存承运商成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("承运商已存在!");
                }
            }

        }


        return j;
    }

    /**
     * 删除车辆
     */
    @ResponseBody
    @RequiresPermissions("tms:truckOwner:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(TruckOwner truckOwner, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        truckOwnerService.delete(truckOwner);
        j.setMsg("删除车辆成功");
        return j;
    }

    /**
     * 批量删除车辆
     */
    @ResponseBody
    @RequiresPermissions("tms:truckOwner:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        String idArray[] = ids.split(",");
        for (String id : idArray) {
            truckOwnerService.delete(truckOwnerService.get(id));
        }
        j.setMsg("删除车辆成功");
        return j;
    }

    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequiresPermissions("tms:truckOwner:export")
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public AjaxJson exportFile(TruckOwner truckOwner, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "车辆" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<TruckOwner> page = truckOwnerService.findPage(new Page<TruckOwner>(request, response, -1), truckOwner);
            new ExportExcel("车辆", TruckOwner.class).setDataList(page.getList()).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出车辆记录失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "detail")
    public TruckOwner detail(String id) {
        return truckOwnerService.get(id);
    }


    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:truckOwner:import")
    @RequestMapping(value = "imports", method = RequestMethod.POST)
    public String importFiles(MultipartFile file, RedirectAttributes redirectAttributes) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<TruckOwner> list = ei.getDataList(TruckOwner.class);
            for (TruckOwner truckOwner : list) {
                try {
                    truckOwnerService.save(truckOwner);
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条车辆记录。");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条车辆记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入车辆失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/truckOwner/?repage";
    }

    /**
     * 下载导入车辆数据模板
     */
    @RequiresPermissions("tms:truckOwner:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "车辆数据导入模板.xlsx";
            List<TruckOwner> list = Lists.newArrayList();
            new ExportExcel("车辆数据", TruckOwner.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/truckOwner/?repage";
    }

    /**
     * 车辆导出数据
     */
    @RequestMapping(value = "exporttruck")
    public ModelAndView export(TruckOwner truckOwner, String ids, HttpServletRequest request, HttpServletResponse response) {

        List<TruckOwner> truckOwnerList = new ArrayList<TruckOwner>();

        if (StringUtils.isNotEmpty(ids)) {
            String idArray[] = ids.split(",");
            for (String id : idArray) {
                truckOwnerList.add(truckOwnerService.get(id));
            }
        } else {
            truckOwnerList = truckOwnerService.findList(truckOwner);
        }

        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<String> titles = new ArrayList<String>();
            //添加订单签收标题
            titles.add("车属单位");
            titles.add("车主姓名");
            titles.add("车主电话");
            titles.add("车主身份证");
            titles.add("车牌号");
            titles.add("车型");
            titles.add("车长（M）");
            titles.add("载重(T)");
            titles.add("最大载货体积(m³)");
            titles.add("运输线路一");
            titles.add("运输线路二");
            titles.add("运输线路三");
            titles.add("车辆购买时间");
            titles.add("车辆品牌");
            titles.add("车辆型号");
            titles.add("合作类型");
            titles.add("驾驶员姓名");
            titles.add("驾驶员电话");
            titles.add("驾驶员身份证");
            titles.add("三者险金额(元)");
            titles.add("货物险金额(元)");
            titles.add("承保公司");
            titles.add("保险购买日期");
            titles.add("保险失效日期");
            titles.add("是否顶置");
            titles.add("置顶到期日期");
            titles.add("浏览数");
            titles.add("分享次数");
            titles.add("录入日期");
            titles.add("修改日期");
            dataMap.put("titles", titles);

            List<PageData> excelList = new ArrayList<PageData>();
            //添加表内容
            for (TruckOwner yq : truckOwnerList) {
                //子表
                List<Trucks> trucksList = new ArrayList<>();

                Trucks trucks = new Trucks();
                List<Trucks> trucksLists = trucksMapper.findList(trucks);
                for (Trucks lineSends : trucksLists) {
                    if ((lineSends.getTruckOwner().getId()).equals(yq.getId())) {
                        trucksList.add(lineSends);
                    }
                }


                if (trucksList.size() > 0) {
                    for (Trucks truck : trucksList) {

                        PageData map = new PageData();
                        if (StringUtils.isNotEmpty(yq.getUnit())) {
                            map.put("var1", yq.getUnit());
                        } else {
                            map.put("var1", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getName())) {
                            map.put("var2", yq.getName());
                        } else {
                            map.put("var2", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getPhone())) {
                            map.put("var3", yq.getPhone());
                        } else {
                            map.put("var3", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getIdCard())) {
                            map.put("var4", yq.getIdCard());
                        } else {
                            map.put("var4", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getNumber())) {
                            map.put("var5", truck.getNumber());
                        } else {
                            map.put("var5", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getType())) {
                            if ("0".equals(truck.getType())) {
                                map.put("var6", "平板");
                            }
                            if ("1".equals(truck.getType())) {
                                map.put("var6", "高栏");
                            }
                            if ("2".equals(truck.getType())) {
                                map.put("var6", "普通箱车");
                            }
                            if ("3".equals(truck.getType())) {
                                map.put("var6", "冷藏箱车");
                            }
                            if ("4".equals(truck.getType())) {
                                map.put("var6", "恒温保温车");
                            }
                            if ("5".equals(truck.getType())) {
                                map.put("var6", "油罐车");
                            }
                            if ("6".equals(truck.getType())) {
                                map.put("var6", "水泥运输罐车");
                            }
                            if ("7".equals(truck.getType())) {
                                map.put("var6", "其他");
                            }
                        } else {
                            map.put("var6", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getLength())) {
                            map.put("var7", truck.getLength());
                        } else {
                            map.put("var7", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getWeight())) {
                            map.put("var8", truck.getWeight());
                        } else {
                            map.put("var8", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getMaxVolume())) {
                            map.put("var9", truck.getMaxVolume());
                        } else {
                            map.put("var9", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getTransportationLineOne())) {
                            map.put("var10", truck.getTransportationLineOne());
                        } else {
                            map.put("var10", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getTransportationLineTwo())) {
                            map.put("var11", truck.getTransportationLineTwo());
                        } else {
                            map.put("var11", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getTransportationLineThree())) {
                            map.put("var12", truck.getTransportationLineThree());
                        } else {
                            map.put("var12", "");
                        }
                        if ((truck.getPurchaseTime()) != null) {
                            map.put("var13", DateUtils.formatDate(truck.getPurchaseTime(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var13", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getVehicleBrand())) {
                            map.put("var14", truck.getVehicleBrand());
                        } else {
                            map.put("var14", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getVehicleModel())) {
                            map.put("var15", truck.getVehicleModel());
                        } else {
                            map.put("var15", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getCooperationType())) {
                            if ("0".equals(truck.getCooperationType())) {
                                map.put("var16", "合同");
                            }
                            if ("1".equals(truck.getCooperationType())) {
                                map.put("var16", "临时");
                            }
                            if ("2".equals(truck.getCooperationType())) {
                                map.put("var16", "自有");
                            }
                        } else {
                            map.put("var16", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getDriver())) {
                            map.put("var17", truck.getDriver());
                        } else {
                            map.put("var17", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getPhone())) {
                            map.put("var18", truck.getPhone());
                        } else {
                            map.put("var18", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getIdCard())) {
                            map.put("var19", truck.getIdCard());
                        } else {
                            map.put("var19", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getThreePartyInsurance())) {
                            map.put("var20", truck.getThreePartyInsurance());
                        } else {
                            map.put("var20", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getCargoInsurance())) {
                            map.put("var21", truck.getCargoInsurance());
                        } else {
                            map.put("var21", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getInsuranceCompany())) {
                            map.put("var22", truck.getInsuranceCompany());
                        } else {
                            map.put("var22", "");
                        }
                        if ((truck.getPurchaseDate()) != null) {
                            map.put("var23", DateUtils.formatDate(truck.getPurchaseDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var23", "");
                        }
                        if ((truck.getExpirationDate()) != null) {
                            map.put("var24", DateUtils.formatDate(truck.getExpirationDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var24", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getTop())) {
                            if (truck.getTop().equals("0")) {
                                map.put("var25", "否");
                            }
                            if (truck.getTop().equals("1")) {
                                map.put("var25", "是");
                            }
                        } else {
                            map.put("var25", "");
                        }
                        if ((truck.getTopDate()) != null) {
                            map.put("var26", DateUtils.formatDate(truck.getTopDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var26", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getSkimAmount())) {
                            map.put("var27", truck.getSkimAmount());
                        } else {
                            map.put("var27", "");
                        }
                        if (StringUtils.isNotEmpty(truck.getSharingTimes())) {
                            map.put("var28", truck.getSharingTimes());
                        } else {
                            map.put("var28", "");
                        }
                        if ((truck.getCreateDate()) != null) {
                            map.put("var29", DateUtils.formatDate(truck.getCreateDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var29", "");
                        }
                        if ((truck.getUpdateDate()) != null) {
                            map.put("var30", DateUtils.formatDate(truck.getUpdateDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var30", "");
                        }
                        excelList.add(map);
                    }
                } else {
                    PageData map = new PageData();
                    if (StringUtils.isNotEmpty(yq.getUnit())) {
                        map.put("var1", yq.getUnit());
                    } else {
                        map.put("var1", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getName())) {
                        map.put("var2", yq.getName());
                    } else {
                        map.put("var2", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getPhone())) {
                        map.put("var3", yq.getPhone());
                    } else {
                        map.put("var3", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getIdCard())) {
                        map.put("var4", yq.getIdCard());
                    } else {
                        map.put("var4", "");
                    }
                    map.put("var5", "");
                    map.put("var6", "");
                    map.put("var6", "");
                    map.put("var7", "");
                    map.put("var8", "");
                    map.put("var9", "");
                    map.put("var10", "");
                    map.put("var11", "");
                    map.put("var12", "");
                    map.put("var13", "");
                    map.put("var14", "");
                    map.put("var15", "");
                    map.put("var16", "");
                    map.put("var17", "");
                    map.put("var18", "");
                    map.put("var19", "");
                    map.put("var20", "");
                    map.put("var21", "");
                    map.put("var22", "");
                    map.put("var23", "");
                    map.put("var24", "");
                    map.put("var25", "");
                    map.put("var26", "");
                    map.put("var27", "");
                    map.put("var28", "");
                    map.put("var29", "");
                    map.put("var30", "");
                    excelList.add(map);
                }
            }

            String fileName = "车辆" + DateUtils.formatDate(new Date(), "yyyy-MM-dd ");
            ;
            dataMap.put("fileName", fileName);

            dataMap.put("excelList", excelList);
            System.out.println("===导出excel成功！");
            ObjectExcelView erv = new ObjectExcelView();                    //执行excel操作

            return new ModelAndView(erv, dataMap);

        } catch (Exception e) {
            System.out.println("======导出excel时出错！" + e.getMessage());
        }
        return new ModelAndView("redirect:" + Global.getAdminPath() + "/tms/storesList");
    }


    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:dropDispatch:import")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<TruckOwnerImport> newList = ei.getDataList(TruckOwnerImport.class);
            for (TruckOwnerImport ddi : newList) {
                try {
                    TruckOwner truckOwner = new TruckOwner();
                    Trucks truck = new Trucks();
                    if (StringUtils.isNotEmpty(ddi.getOwnerUnit())) {
                        truckOwner.setUnit(ddi.getOwnerUnit());
                    }
                    if (StringUtils.isNotEmpty(ddi.getOwnerName())) {
                        truckOwner.setName(ddi.getOwnerName());
                    }
                    if (StringUtils.isNotEmpty(ddi.getOwnerPhone())) {
                        truckOwner.setPhone(ddi.getOwnerPhone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getOwnerId())) {
                        truckOwner.setIdCard(ddi.getOwnerId());
                    }
                    //子表
                    if (StringUtils.isNotEmpty(ddi.getNumber())) {
                        truck.setNumber(ddi.getNumber());
                    }
                    if (StringUtils.isNotEmpty(ddi.getType())) {
                        if ("平板".equals(ddi.getType())) {
                            truck.setType("0");
                        }
                        if ("高栏".equals(ddi.getType())) {
                            truck.setType("1");
                        }
                        if ("普通箱车".equals(ddi.getType())) {
                            truck.setType("2");
                        }
                        if ("冷藏箱车".equals(ddi.getType())) {
                            truck.setType("3");
                        }
                        if ("恒温保温车".equals(ddi.getType())) {
                            truck.setType("4");
                        }
                        if ("油罐车".equals(ddi.getType())) {
                            truck.setType("5");
                        }
                        if ("水泥运输罐车".equals(ddi.getType())) {
                            truck.setType("6");
                        }
                        if ("其他".equals(ddi.getType())) {
                            truck.setType("7");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getLength())) {
                        truck.setLength(ddi.getLength());
                    }
                    if (StringUtils.isNotEmpty(ddi.getWeight())) {
                        truck.setWeight(ddi.getWeight());
                    }
                    if (StringUtils.isNotEmpty(ddi.getMaxVolume())) {
                        truck.setMaxVolume(ddi.getMaxVolume());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTransportationLineOne())) {
                        truck.setTransportationLineOne(ddi.getTransportationLineOne());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTransportationLineTwo())) {
                        truck.setTransportationLineTwo(ddi.getTransportationLineTwo());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTransportationLineThree())) {
                        truck.setTransportationLineThree(ddi.getTransportationLineThree());
                    }
                    if (StringUtils.isNotEmpty(ddi.getPurchaseTime())) {
                        truck.setPurchaseTime(df.parse(ddi.getPurchaseTime()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getVehicleBrand())) {
                        truck.setVehicleBrand(ddi.getVehicleBrand());
                    }
                    if (StringUtils.isNotEmpty(ddi.getVehicleModel())) {
                        truck.setVehicleModel(ddi.getVehicleModel());
                    }
                    if (StringUtils.isNotEmpty(ddi.getCooperationType())) {
                        if ("合同".equals(ddi.getCooperationType())) {
                            truck.setCooperationType("0");
                        }
                        if ("临时".equals(ddi.getCooperationType())) {
                            truck.setCooperationType("1");
                        }
                        if ("自有".equals(ddi.getCooperationType())) {
                            truck.setCooperationType("2");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getDriver())) {
                        truck.setDriver(ddi.getDriver());
                    }
                    if (StringUtils.isNotEmpty(ddi.getPhone())) {
                        truck.setPhone(ddi.getPhone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getIdCard())) {
                        truck.setIdCard(ddi.getIdCard());
                    }
                    if (StringUtils.isNotEmpty(ddi.getThreePartyInsurance())) {
                        truck.setThreePartyInsurance(ddi.getThreePartyInsurance());
                    }
                    if (StringUtils.isNotEmpty(ddi.getCargoInsurance())) {
                        truck.setCargoInsurance(ddi.getCargoInsurance());
                    }
                    if (StringUtils.isNotEmpty(ddi.getInsuranceCompany())) {
                        truck.setInsuranceCompany(ddi.getInsuranceCompany());
                    }
                    if (StringUtils.isNotEmpty(ddi.getPurchaseDate())) {
                        truck.setPurchaseDate(df.parse(ddi.getPurchaseDate()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getExpirationDate())) {
                        truck.setExpirationDate(df.parse(ddi.getExpirationDate()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getTop())) {
                        if (ddi.getTop().equals("否")) {
                            truck.setTop("0");
                        } else if (ddi.getTop().equals("是")) {
                            truck.setTop("1");
                        } else {
                            truck.setTop("");
                        }

                    }
                    if ((ddi.getTopDate()) != null) {
                        truck.setTopDate(df.parse(ddi.getTopDate()));
                    }
                    TruckOwner truckOwnercx = new TruckOwner();
                    truckOwnercx.setName(truckOwner.getName());
                    List<TruckOwner> truckOwnerList = truckOwnerService.findList(truckOwnercx);
                    if (truckOwnerList.size() > 0) {
                        truck.setTruckOwner(truckOwnerList.get(0));
                        truckOwnerService.saveTrucks(truck);
                    } else {
                        truckOwnerService.save(truckOwner);
                        truck.setTruckOwner(truckOwner);
                        truckOwnerService.saveTrucks(truck);
                    }
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条订单记录。失败原因例如：导入数据出错！");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条订单记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入订单失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/truckOwner/?repage";
    }

}